package org.shoukaiseki.initdict.handle;

import org.shoukaiseki.common.utils.ListUtils;
import org.shoukaiseki.initdict.annotation.DictTypeEnumPackage;
import org.shoukaiseki.initdict.logger.InitDictLoggers;
import org.slf4j.Logger;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.util.List;
import java.util.Set;

/**
 * 扫描
 * com.autumn.initdict.handle.DictTypeEnumPackageScan
 * <br>
 *
 * @author shoukaiseki ブログ http://www.shoukaiseki.top/<br> E-メール jiang28555@Gmail.com<br>
 * @date 2020-12-22 12:19:45<br>
 */
public class DictTypeEnumPackageScan implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {


    private static List<Class> dictTypeClassList = ListUtils.newArrayList();


    public static final Logger logger = InitDictLoggers.DEFAULT_LOGGER;

    ResourceLoader resourceLoader;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(DictTypeEnumPackage.class.getName()));
        String[] basePackages = annoAttrs.getStringArray("basePackage");
        //自定义的 包扫描器
        DictTypeEnumPackageScanHandle scanHandle = new DictTypeEnumPackageScanHandle(beanDefinitionRegistry,false);
        //扫描指定路径下的接口
        Set<Class> beanDefinitionHolders = scanHandle.doScan(basePackages);
        dictTypeClassList.addAll(beanDefinitionHolders);
    }

    public static List<Class> getDictTypeClassList() {
        return dictTypeClassList;
    }
}
